package com.app.freshmenapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class RegistActivity extends AppCompatActivity {
    EditText UserName,Password,Name,RePassword,Email,Phone;
    Button Regist;
    Spinner mSpDepartment,mSpMajor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        initView();
        initEvent();
    }

    private void initEvent() {
        Regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                regist();
            }
        });
    }

    private void regist() {
        //创建请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        //创建请求
        final JSONObject Object = new JSONObject();

        try {
            Object.put("name",UserName.getText().toString());
            Object.put("password",Password.getText().toString());
            Object.put("email",Email.getText().toString());
            Object.put("telephone",Phone.getText().toString());
            Object.put("department",mSpDepartment.getSelectedItem().toString());
            Object.put("major",mSpMajor.getSelectedItem().toString());
            Object.put("role","学生");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST,
                "http://106.12.30.77:8083/freshmenapp/user/regist",
                Object,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        boolean success = jsonObject.optBoolean("success",false);
                        if (success==true){
                            Toast.makeText(RegistActivity.this, "成功注册", Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(RegistActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(RegistActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                }

        );

        requestQueue.add(jsonObjectRequest);
    }


    private void initView() {
        UserName = findViewById(R.id.mEt_username);
        Password = findViewById(R.id.mEt_password);
        Name = findViewById(R.id.mEt_name);
        RePassword = findViewById(R.id.mEt_repassword);
        Email = findViewById(R.id.mEt_email);
        Phone = findViewById(R.id.mEt_phone);
        Regist = findViewById(R.id.btn_regist);
        mSpDepartment = findViewById(R.id.sp_department);
        mSpMajor = findViewById(R.id.sp_major);
    }
}
